#!/usr/bin/python
# -*- coding: utf-8 -*-
#===============================================================================
# Copyright 2011 zod.yslin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# 
# Author: zod.yslin
# Email: 
# File Name: lamba-map.py
# Description: 
# 
# Edit History: 
#   2011-07-29    File created.
#===============================================================================
def plus1(x, y):
    return x + y

plus2 = lambda x, y: x + y
print plus2(1, 2) # print 3

def show1():
    return 'X'

show2 = lambda: 'X'
print(show2())

def print_list(alist):
    for i in alist:
        print i,
    print

list = xrange(0,10,2)  # 0 2 4 ... 8

# Let's redefine increment as a lambda function.
increment = lambda x: x + 1

# This looks like how you would normally do it.
map( increment, list )
print_list(list)

# Or, we're do lazy for that "=" statement above :~)
# That and increment is so simple...
map( lambda x: x + 1, list )
print_list(list)

# Where lambda won't work:
# Lambda's cannot contain statements.  So although the following
# code is almost valid Ocaml, it is _not_ valid Python.  :~)
#lmap = ( lambda f, lst:
#    if lst == []:
#        return []
#    else:
#        return [ f( lst[0] ) ] + lmap( f lst[1:] ) )

lmap = ( lambda f, lst: [] if lst == [] else [ f( lst[0] ) ] + lmap( f, lst[1:] ) )
print(lmap(increment, [1,2,3]))
